<template>
  <div class="bigbox">
   <!-- 页面导航搜索盒子 -->
    <div class="topbox">
      <el-form ref="form" label-position="left" label-width="80px">
          <el-row :gutter="20">
              <el-col :span="5">
                <el-form-item label="建筑群">
                  <FilterSelect2
                    v-model="buildingId"
                    :optionlist="qiyelist"
                    @updateModelValue="changeqiye"
                    :valuekey="{ name: 'name', value: 'id' }"
                  ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="记录日期">
                  <el-date-picker
                    style="width: 100%;"
                    size="small"
                    v-model="value1"
                    value-format="yyyy-MM-dd"
                    type="daterange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="电表名称">
                  <FilterSelect2
                    v-model="meterId"
                    :optionlist="meterlist"
                    :valuekey="{ name: 'meterName', value: 'meterId' }"
                  ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="8" style="text-align:right">
                <el-form-item label="">
                  <el-button @click="chongzhi" size="small">重 置</el-button>
                  <el-button @click="gettablelist()" type="primary" size="small">查 询</el-button>
                </el-form-item>
              </el-col>
          </el-row>
      </el-form>
    </div> 
    <!-- 盒子主体部分 -->
    <div class="bodybox">
      <div class="title">
        <div>操作记录</div>
        <div>
          <TopTile  @command="command" @daochu="daochu" @shuaxin="shuaxin"></TopTile>
        </div>
      </div>
      <div class="table">
        <TableColumn
        ref="tabledata"
        :tableData="tableData" 
        :tableConfig="tableConfig" 
        :issize="issize">
        <el-table-column
         align="center"
         prop="operationType"
         label="动作名称" >
         <template #default="{row}">
              <span v-if="row.operationType=='通电'" class="states green"></span>
              <span v-else class="states red"></span>
              <span>{{ row.operationType}}</span>
            </template>
       </el-table-column>
       <el-table-column
         align="center"
         width="180"
         prop="operationTime"
         label="动作时间" >
       </el-table-column>
       <el-table-column
         align="center"
         prop="sendStatus"
         label="动作结果" >
         <template #default="{row}">
              <span v-if="row.operationResult=='成功'" class="states green"></span>
              <span v-else class="states red"></span>
              <span>{{ row.operationResult}}</span>
            </template>
       </el-table-column>
       <el-table-column
         prop="feeControlType"
         label="方式" >
       </el-table-column>
       <el-table-column
         prop="operationUserName"
         label="操作人" >
       </el-table-column>
       </TableColumn>
      </div>
     <!-- 底部分页 -->
      <div class="fenye">
          <el-pagination
                background
                layout="prev, pager, next"
                :current-page.sync="pagejilu.pageNo"
                :page-size="20"
                @current-change="gettablelist"
                :total="+totalCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {gettablelistapis} from '@/api/prwpaid/meter-management/meter-record'
import {gettablelistapi} from '@/api/prwpaid/file-management/meter'
import {getloginlistapi} from '@/api/prwpaid/file-management/Architectural-complex'
import TopTile from '@/components/TopTitle/index.vue'
import TableColumn from '@/components/Table/index.vue'
import {exportexcel} from '@/utils/excel'
export default {
  name: 'CallPoliceIndex',
  components: {
    TopTile,
    TableColumn
  },
  data() {
    return {
      meterId:'',
      issize: 'medium',
      buildingId:'',
      value1:['',''],
      meterlist: [],
      qiyelist: [],
      tableData: [],
      tableConfig: [
      { label: '序号', type:'index' ,center:'center',width:'50'},
        { label: '建筑群名称', prop: 'buildingName' ,width:'200'},
        { label: '记录日期', prop: 'recordTime',center:'center',width:'180' },
        { label: '电表名称', prop: 'meterName' },
        { label: '房间名称', prop: 'roomName' },
        { label: '房间编号', prop: 'roomCode' },
        { label: '用户名称', prop: 'customerName' },
        { label: '用户编号', prop: 'customerCode' },
        // { label: '动作名称', prop: 'operationType' },
        // { label: '动作时间', prop: 'operationTime' ,center:'center',width:'180'},
        // { label: '动作结果', prop: 'operationResult' },
        // { label: '方式', prop: 'feeControlType' },
        // { label: '操作人', prop: 'operationUserName' },
      ],
      pagejilu: {
        pageNo: 1,
        pageSize:20
      },
      totalCount:''
    };
  },

  async mounted() {

    // 获取建筑群
    const res = await getloginlistapi()
    this.qiyelist = res.data.list
    const localbuildingId = localStorage.getItem('buildingId')
    const isBuildIdInArray = this.qiyelist.some(item => item.id == localbuildingId);
    if(isBuildIdInArray){
      this.buildingId = localbuildingId ? localbuildingId : ''
    }else{
      this.buildingId = ''
    }

    const meter = await gettablelistapi({ buildingId:this.buildingId,roomId:'',name:'',gatewayName:'',status:'', pageNo:0, pageSize: 500 })
      this.meterlist = meter.data.list
    this.gettablelist()
  },

  methods: {
    // 企业下拉
    async changeqiye(val) {
      // 获取表计
      this.meterId=''
      const meter = await gettablelistapi({ buildingId:val,roomId:'',name:'',gatewayName:'',status:'', pageNo:0, pageSize: 500 })
      this.meterlist = meter.data.list
      localStorage.setItem('buildingId', val)
    },
    // 获取表格信息
    async gettablelist() {
    this.$refs.tabledata.loading=true
      const { data: { list,totalCount } } = await gettablelistapis({
        buildingId: this.buildingId, meterId: this.meterId,
        startDate: this.value1[0], endDate: this.value1[1],
        pageNo:this.pagejilu.pageNo-1, pageSize: this.pagejilu.pageSize
      })
      this.totalCount = totalCount
      this.tableData = list
      this.$refs.tabledata.loading=false
    },
    // 重置
    chongzhi() {
      localStorage.removeItem('buildingId');
      this.buildingId = ''
      this.meterId = ''
      this.value1 = ['', '']
      this.gettablelist()
    },
 // 调节表格密度
  command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    // 导出
    daochu() {
      exportexcel('mbm-microservice-biz/mbm/meter/operation/record/exportExcel', {
        buildingId: this.buildingId, meterId: this.meterId,
        startDate: this.value1[0], endDate: this.value1[1],
      },'表计操作记录')
    },
    // 刷新列表
    shuaxin() {
      this.gettablelist()
      this.$message.success('列表刷新成功')
    },
  },
};
</script>

<style lang="scss" scoped>
.option{
  padding-left: 10px;
}
.bigbox{
  height: 100%;
  padding: 10px;
  .topbox{
    height: 80px;
    padding: 20px;
    background-color: #fff;
    margin-bottom: 10px;
  }
  .bodybox{
    height: calc(100% - 90px);
    padding: 20px;
    background-color: #fff;
    .title{
      margin-bottom: 10px;
      display: flex;
      justify-content: space-between;
    }
    .table{
      height: 88%;
      .states{
        display: inline-block;
        width: 10px;
        height: 10px;
        margin-right: 10px;
        border-radius: 50%;
      }
      .red{
        background-color:red;
      }
      .green{
        background-color: #13ce66;
      }
    }
    .fenye{
      text-align: right;
      margin-top: 10px;
    }
  }
}
</style>